using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Diagnostics;

using Softelvdm.SftTreeNET;
using Softelvdm.Controls;

namespace XMLProps
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("..\\..\\XmlFile.xml");

            sftTree1.Headers[0,0].Text = "Options";
            sftTree1.RowHeaders.Width = 0;
            ProcessNodes(doc.ChildNodes, null);

            sftTree1.Columns.MakeOptimal(0, false);
            sftTree1.RecalcHorizontalExtent();

        }

        private void ProcessNodes(XmlNodeList xmlNodeList, ItemClass item)
        {
            // Process all nodes (recursively if necessary)
            foreach (XmlNode elem in xmlNodeList)
            {
                string id = "";
                string desc = "";
                try {
                    id = elem.Attributes["ID"].Value;
                    desc = elem.Attributes["Desc"].Value;
                } catch {
                    id = desc = "";
                }
                if (id != "" && desc != "") {
                    ItemClass newItem = ProcessNode(item, elem, id, desc);
                    if (elem.ChildNodes != null)
                        ProcessNodes(elem.ChildNodes, newItem);
                } else {
                    if (elem.ChildNodes != null)
                        ProcessNodes(elem.ChildNodes, item);
                }
            }
        }

        // Process a node, looking for checkboxes and radiobuttons, everyhing else 
        // is a section title
        private ItemClass ProcessNode(ItemClass item, XmlNode elem, string id, string desc)
        {
            ItemClass newItem;
            if (item != null)
                newItem = item.Add();
            else
                newItem = sftTree1.ItemCollection.Add();
            newItem.TagObject = id;
            if (elem.Name == "Checkbox") {
                CheckBoxStateEnum check = CheckBoxStateEnum.Unchecked;
                string strcheck = "";
                try {
                    strcheck = elem.Attributes["Status"].Value;
                } catch {
                    strcheck = "";
                }
                if (strcheck != "")
                    check = (strcheck != "0") ? CheckBoxStateEnum.Checked : CheckBoxStateEnum.Unchecked;
                newItem.Cells[0].Parts.Add(new CheckBoxPartClass(check));
            } else if (elem.Name == "Radiobutton") {
                RadioButtonStateEnum check = RadioButtonStateEnum.Unchecked;
                string strcheck = "";
                try {
                    strcheck = elem.Attributes["Status"].Value;
                } catch {
                    strcheck = "";
                }
                if (strcheck != "")
                    check = (strcheck != "0") ? RadioButtonStateEnum.Checked : RadioButtonStateEnum.Unchecked;
                newItem.Cells[0].Parts.Add(new RadioButtonPartClass(check));
            }
            newItem.Cells[0].Text = desc;
            return newItem;
        }

        // Handle the space char to select the checkbox or radiobutton
        private void sftTree1_KeyPress(object sender, KeyPressEventArgs e) {
            if (e.KeyChar == ' ') {
                ItemClass item = sftTree1.FocusItem;
                if (item != null) {
                    GenericPartClass gp = item.Cells[0].Parts[0];
                    if (gp is CheckBoxPartClass) {
                        CheckBoxPartClass cb = (CheckBoxPartClass) gp;
                        cb.State = cb.State == CheckBoxStateEnum.Checked ? CheckBoxStateEnum.Unchecked : CheckBoxStateEnum.Checked;
                        e.Handled = true;
                        // changing the checkbox state doesn't automatically call the CheckBoxClicked event
                        CellClass owningCell = (CellClass)cb.PartOwner;
                        ItemClass owningItem = owningCell.OwningItem;
                        sftTree1_CheckBoxClicked(this, new PartEventArgs(owningItem, owningCell, null, cb));
                    } else if (gp is RadioButtonPartClass) {
                        RadioButtonPartClass rb = (RadioButtonPartClass)gp;
                        if (sftTree1.PerformClick_RadioButtonPart(rb)) {
                            e.Handled = true;
                            // changing the radiobutton state doesn't automatically call the RadioButtonClicked event
                            CellClass owningCell = (CellClass) rb.PartOwner;
                            ItemClass owningItem = owningCell.OwningItem;
                            sftTree1_RadioButtonClicked(this, new PartEventArgs(owningItem, owningCell, null, rb));
                        }
                    }
                }
            }
        }

        // Handle the space char, it should select the checkbox or radiobutton
        // instead of the tree control's default action which is to change the 
        // item's selection state.
        private void sftTree1_KeyDown(object sender, KeyEventArgs e) {
            if (e.KeyCode == Keys.Space) {
                e.Handled = true;
            }
        }

        private void sftTree1_CheckBoxClicked(object sender, PartEventArgs e) {
            Debug.Print("The checkbox labeled " + e.Cell.Text + " was clicked.");
        }

        private void sftTree1_RadioButtonClicked(object sender, PartEventArgs e) {
            Debug.Print("The radiobutton labeled " + e.Cell.Text + " was clicked.");
        }

    }
}